package com.campus.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.constant.ActivityStatus;
import com.campus.entry.ManagerActivity;
import com.campus.pojo.dto.ManagerActivityDto;
import com.campus.response.ResponseResult;
import com.campus.response.ResultCode;
import com.campus.service.CourseService;
import com.campus.service.ManagerActivityService;
import com.campus.service.ManagerStudentService;
import com.campus.service.ManagerTeacherService;
import com.campus.util.GetUserIdUtil;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ManagerActivityController {
    @Autowired
    private ManagerActivityService managerActivityService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ManagerStudentService studentService;
    @Autowired
    private ManagerTeacherService teacherService;

    /**
     * 创建一个活动
     * @param managerActivity
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PostMapping("/create")
    public ResponseResult<Object> createActivity(@RequestBody ManagerActivity managerActivity){
        managerActivity.setCreator(String.valueOf(GetUserIdUtil.getUserId()));
        managerActivity.setCreatedTime(LocalDateTime.now());
        managerActivityService.saveActivity(managerActivity);

        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 更新活动
     * @param managerActivity
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/update")
    public ResponseResult<Object> updateActivity(@RequestBody ManagerActivity managerActivity){
        managerActivityService.updateActivity(managerActivity);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);

    }

    /**
     * 开放活动
     * @param activityId
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/publish")
    public ResponseResult<Object> publishActivity(@RequestParam Integer activityId){
        managerActivityService.changeActivityStatus(activityId, ActivityStatus.PUBLISH_ACTIVITY);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**关闭活动
     * @param activityId
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @PutMapping("/close")
    public ResponseResult<Object> closeActivity(@RequestParam Integer activityId){
        managerActivityService.changeActivityStatus(activityId,ActivityStatus.END_ACTIVITY);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 根据I删除活动
     * @param activityId
     * @return {@link ResponseResult }<{@link Object }>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @DeleteMapping("/delete")
    public ResponseResult<Object> deleteActivity(@RequestParam Integer activityId){
        managerActivityService.changeActivityStatus(activityId,ActivityStatus.DELETE_ACTIVITY);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,null);
    }

    /**
     * 模糊查询活动列表
     * @param activityName
     * @param status
     * @param pageNum
     * @param pageSize
     * @return {@link ResponseResult }<{@link Page }<{@link ManagerActivity }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/list")
    public ResponseResult<Page<ManagerActivity>> listActivity(
            @RequestParam(required = false) String activityName, // 活动名称
            @RequestParam(required = false) Integer status,    // 活动状态
            @RequestParam(defaultValue = "1") Integer pageNum, // 当前页
            @RequestParam(defaultValue = "10") Integer pageSize // 每页条数
    ) {
        // 创建 ManagerActivityDto 对象，包含查询条件
        ManagerActivityDto managerActivityDto = new ManagerActivityDto();
        managerActivityDto.setActivityName(activityName);
        managerActivityDto.setStatus(status);

        // 调用 Service 层的分页查询方法
        Page<ManagerActivity> page = managerActivityService.listManagerActivity(managerActivityDto, pageNum, pageSize);

        // 返回分页结果
        return new ResponseResult<>(ResultCode.ALL_SUCCESS, page);
    }

    /**
     * 根据活动Id查询活动
     * @param id
     * @return {@link ResponseResult }<{@link ManagerActivity }>
     */
    @GetMapping("/{id}")
    public ResponseResult<ManagerActivity> getActivityById(@PathVariable Integer id){
        ManagerActivity managerActivity=managerActivityService.getActivityById(id);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,managerActivity);
    }

    /**
     * 查询教师，学员，活动，课程数量
     * @return {@link ResponseResult }<{@link HashMap }<{@link String }, {@link Long }>>
     */
    @PreAuthorize("hasAnyRole({'admin'})")
    @GetMapping("/admin/count")
    public ResponseResult<HashMap<String, Long>> countActivity(){
        Long contActivity = managerActivityService.contActivity();
        Long countCourse=courseService.countCourse();
        Long countStudent=studentService.countStudent();
        Long conutTeacher=teacherService.conutTeacher();
        HashMap<String,Long> hashMap=new HashMap<>();
        hashMap.put("activity",contActivity);
        hashMap.put("course",countCourse);
        hashMap.put("student",countStudent);
        hashMap.put("teacher",conutTeacher);
        return new ResponseResult<>(ResultCode.ALL_SUCCESS,hashMap);

    }

}
